package com.mall507.fivepeaks.activity.mine;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.hzh.frame.ui.activity.AbsRecyclerViewUI;
import com.hzh.frame.widget.xrecyclerview.RecyclerViewHolder;
import com.mall507.comn.model.MineProblem;
import com.mall507.comn.model.MineRelease;
import com.mall507.comn.model.User;
import com.mall507.comn.tools.UserTools;
import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.widget.imageview.CircleImageView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @author
 * @version 1.0
 * @date 2019/8/27
 */
@Route(path = "/mine/ProblemRUI")
public class ProblemRUI extends AbsRecyclerViewUI<MineProblem>{

    @Override
    protected int setLayoutId() {
        return R.layout.fm_rv_mine_problem_index;
    }

    @Override
    protected void bindView() {
        showLoding();
        getTitleView().setContent("我的问题");
    }
    
    @Override
    protected String setHttpPath() {
        return "Me/getQuestion";
    }

    @Override
    protected List<MineProblem> handleHttpData(JSONObject response) {
        List<MineProblem> list = new ArrayList<>();
        if(200==response.optInt("code")){
            JSONArray jsonArray=response.optJSONArray("data");
            for(int i=0;i<jsonArray.length();i++){
                JSONObject object=jsonArray.optJSONObject(i);
                MineProblem model=new MineProblem();
                model.setNid(object.optString("id"));
                model.setStatus(object.optString("status"));
                model.setTime(object.optString("create_at"));
                model.setTitle(object.optString("title"));
                model.setImageUrl(object.optString("cover_img"));
                model.setBrowseNumber(object.optString("views"));
                model.setComment_count(object.optString("comment_count"));
                list.add(model);
            }
        }
        dismissLoding();
        return list;
    }

    @Override
    protected int setItemLayoutId(int viewType) {
        return R.layout.item_rv_mine_problem;
    }

    @Override
    protected void bindItemData(RecyclerViewHolder holder, int position, final MineProblem model) {
        int str = Integer.parseInt(model.getComment_count());
        TextView status = holder.itemView.findViewById(R.id.status);
        if (str > 0){
            status.setText("已回答");
        }else {
            status.setText("未回答");
            status.setVisibility(View.VISIBLE);
            status.setTextColor(getResources().getColor(R.color.red));
        }
        if (UserTools.getInstance().getIsLogin()) {
            User user = UserTools.getInstance().getUser();
            holder.setText(R.id.nice,user.getNickName());
            ((CircleImageView) holder.getView(R.id.img_avatar)).setImageURI(user.getHead());
        } else {
            User user = UserTools.getInstance().getUser();
            holder.setText(R.id.nice,user.getNickName());
            ((CircleImageView) holder.getView(R.id.img_avatar)).setImageURI(user.getHead());
        }
        holder.setText(R.id.time,model.getTime());
        holder.setText(R.id.title,model.getTitle());
        holder.setText(R.id.browseNumber,model.getBrowseNumber() + "人浏览");
        holder.setOnItemClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance()
                        .build("/comn/ComnQuestionItemInfoRUI")
                        .withString("id", model.getNid())
                        .withString("type","8")
                        .withString("commentRequestPath","Comment/getQuestionComment")
                        .withString("headRequestPath","Community/getQuestion")
                        .navigation();
            }
        });
    }

    @Override
    protected void onRestart() {
        onRefresh();
        super.onRestart();
    }
}
